<template>
  <a-modal :open="open" width="1000px" title="危急值警告" centered :keyboard="false" @ok="onOk" @cancel="onCancel">
    <div class="flex flex-col">
      <div class="bg-secondary w-full p-4 rounded-md border border-solid border-primary mb-4">
        <div class="flex items-end">
          <div class="flex items-end w-[240px]">
            <div class="text-lg font-semibold text-primary">{{ crisisInfo.brxxbEntity.brName }}</div>
            <div class="mx-3">{{ crisisInfo.brxxbEntity.brXb }}</div>
            <div>{{ crisisInfo.brxxbEntity.brAge || '-' }}</div>
          </div>
          <LabelValue label="民族" :width="240" :value="crisisInfo.brxxbEntity.brMz" />
          <LabelValue label="出生日期" :width="240" :value="crisisInfo.brxxbEntity.brBtd" />
        </div>
        <div class="flex items-center mt-2">
          <LabelValue label="人员编号" :width="240" :value="crisisInfo.brxxbEntity.brId" />
          <LabelValue label="证件类型" :width="240" :value="crisisInfo.brxxbEntity.brZjlx" />
          <LabelValue label="证件号码" :width="240" :value="crisisInfo.brxxbEntity.brSfz" />
        </div>
      </div>
      <div class="mb-4">
        <div class="flex items-center mt-2">
          <LabelValue label="检验大类" :width="360" :value="crisisInfo.lisRecordEntity?.categroyName" />
          <LabelValue label="检验时间" :width="240" :value="crisisInfo.lisRecordEntity?.testTime" />
        </div>
        <div class="flex items-center mt-2">
          <LabelValue label="处理方式" :width="360" :value="crisisInfo.lisRecordEntity?.crisisLisCodeName" />
          <LabelValue label="备注" :width="240" :value="crisisInfo.lisRecordEntity?.crisisLisMemo" />
        </div>
      </div>
      <BaseTable
        :height="200"
        :data-source="crisisInfo.lisRecordItemEntityList"
        :scroll="{ x: 400, y: 200 }"
        :loading="loading"
        column-code="00001116"
        :exclude-columns="['action', 'lastResult']"
        ex
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            <div class="flex justify-between">
              {{ index + 1 }}
              <RoundTag v-if="record.crisisStatus === '1'" bg-color="#f00">危</RoundTag>
            </div>
          </template>
          <template v-if="column.dataIndex === 'testResult'">
            <div class="flex items-center">
              <div
                class="pr-8"
                :class="{
                  'text-red-500': isOver(record.testResult, record.maxValue) || record.testResult === '阳',
                  'text-green-500': isLower(record.testResult, record.minValue)
                }"
              >
                {{ record.testResult }}
              </div>
              <div v-if="record.testResult" class="tip absolute right-4 text-2xl top-2 z-10">
                <div v-if="isOver(record.testResult, record.maxValue)" class="text-red-500">↑</div>
                <div v-else-if="isLower(record.testResult, record.minValue)" class="text-green-500">↓</div>
              </div>
            </div>
          </template>
        </template>
      </BaseTable>
      <a-form-item label="" class="w-full mt-4">
        <a-input v-model:value="formState.crisisLisMemo" placeholder="请输入处理意见" />
      </a-form-item>
    </div>
  </a-modal>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'

const formState = ref({})
const open = ref(false)
const crisisInfo = ref({})
const isLower = (a, b) => Number(b) && Number(a) < Number(b)
const isOver = (a, b) => Number(b) && Number(a) > Number(b)
const getCrisisList = async () => {
  const { code, msg, data } = await post('/lis/LisRecord/getLisCrisisRecordList4ApplyDoctor', {})
  if (code !== 0) return message.error(msg)
  if (data.length > 0) {
    open.value = true
    crisisInfo.value = data[0]
  }
}
const onCancel = () => {
  formState.value = {}
}
const onOk = async () => {
  const { code, msg } = await post('/lis/LisRecord/applyDoctorConfirmCrisisLisRecord', {
    lisRecordId: crisisInfo.value.lisRecordEntity.id,
    crisisLisMemo: formState.value.crisisLisMemo
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  open.value = false
  getCrisisList()
}
onMounted(() => {
  getCrisisList()
})
</script>
<style lang="less" scoped></style>
